# 1.协程
# A函数调用B函数后，可以在B函数任意地方返回接着执行A函数。此时还可以再调用B函数，从B函数刚才返回的地方接着执行。
# 区别：
# 子程序：与协程的区别是，调用B函数，得等函数体执行完了才会返回。每次都是从B函数头开始执行的（协程的一种）

# fun.send(None) 开始调用程序处使用，fun.send（n） 正常调用程序  ， n = yield n 执行处标记或者返回标记处（前面参数接收值，后面参数是返回值）

# 核心点在于调度那块由他来负责解决，遇到阻塞操作，立刻yield掉，并且记录当前栈上的数据，阻塞完后立刻再找一个线程恢复栈?并把阻塞的结果放到这个线程上去跑

# 协程用于解决IO堵塞。 当Cup运算时遇到IO操作 需要等待IO操作回来，这个时候Cpu也时跟着等待的，这里用协程来办的事情就是跳转其他程序继续干活，等堵塞结束再回来。

# https://blog.csdn.net/chunzhenzyd/article/details/76270953
# https://blog.csdn.net/qiansg123/article/details/80123051

# 协程  同一个线程中的两个函数切换执行
import asyncio
import threading


def func1():
    a = yield
    print("aaafunc1aa1", a)
    b = yield "aaaa2返回的数据"  # 先接收其他程序qiqi
    print("aaaa3", b)
    yield


def func2(func):
    x = func.send(None)  # 启动生成器  无值返回
    print("bbb1", x)
    y = func.send("bbb2发送数据")  # send 带数据切换到其他程序，并且接收其他程序切换回来时的参数
    print("bbb3", y)
    z = func.send("bbb4发送数据")
    print("bbb5", z)
    func.close()

func2(func1())

#协程框架asyncio
@asyncio.coroutine
def sl():
    asyncio.sleep(1)
    print("slslsls %s" %threading.currentThread())
@asyncio.coroutine
def func3():
    print("1111 %s" %threading.currentThread())
    yield from asyncio.sleep(1)
    print("2222 %s" %threading.currentThread())

loop = asyncio.get_event_loop()
tasks = [func3(), func3()]
loop.run_until_complete(asyncio.wait(tasks))
loop.close()


# @asyncio.coroutine
# def hello():
#     print('Hello world! (%s)' % threading.currentThread())
#     yield from asyncio.sleep(1)
#     print('Hello again! (%s)' % threading.currentThread())
#
# loop = asyncio.get_event_loop()
# tasks = [hello(), hello()]
# loop.run_until_complete(asyncio.wait(tasks))
# loop.close()



# import asyncio
#
# from aiohttp import web
#
#
# @asyncio.coroutine
# def index(request):
#     yield from asyncio.sleep(0.5)
#     return web.Response(body=b'<h1>Index</h1>')
#
#
# @asyncio.coroutine
# def hello(request):
#     yield from asyncio.sleep(0.5)
#     text = '<h1>hello, %s!</h1>' % request.match_info['name']
#     return web.Response(body=text.encode('utf-8'))
#
#
# @asyncio.coroutine
# def init(loop):
#     app = web.Application(loop=loop)
#     app.router.add_route('GET', '/', index)
#     app.router.add_route('GET', '/hello/{name}', hello)
#     srv = yield from loop.create_server(app.make_handler(), '127.0.0.1', 8000)
#     print('Server started at http://127.0.0.1:8000...')
#     return srv
#
#
# loop = asyncio.get_event_loop()
# loop.run_until_complete(init(loop))




# import logging; logging.basicConfig(level=logging.INFO)
#
# import asyncio, os, json, time
# from datetime import datetime
#
# from aiohttp import web
#
# def index(request):
#     return web.Response(body=b'<h1>Awesome</h1>')
#
# @asyncio.coroutine
# def init(loop):
#     app = web.Application(loop=loop)
#     app.router.add_route('GET', '/', index)
#     srv = yield from loop.create_server(app.make_handler(), '127.0.0.1', 9000)
#     logging.info('server started at http://127.0.0.1:9000...')
#     return srv
#
# loop = asyncio.get_event_loop()
# loop.run_until_complete(init(loop))
# loop.run_forever()
